<template>
  <d2-container>
    <el-main>

      <el-form ref="staffForm" :model="staffForm" label-width="120px" label-position="left" :rules="formRules">

        <el-form-item label="转发内容" prop="forwardStr">
          <el-input placeholder="请输入转发内容" v-model="staffForm.forwardStr" :disabled="disabled"></el-input>
        </el-form-item>

        <el-form-item label="url" prop="url">
          <el-input placeholder="请输入url" v-model="staffForm.url" :disabled="disabled"></el-input>
        </el-form-item>


        <el-footer style="text-align: center">
          <el-button type="primary" @click="submit">提交</el-button>
          <el-button @click="resetForm('staffForm')">重置</el-button>
          <el-button @click="edit" v-show="editShow">修改</el-button>
        </el-footer>


      </el-form>

    </el-main>
  </d2-container>
</template>

<script>
import request from "@/plugin/axios";
import { mapState } from "vuex"
export default {
  data() {
    return {
      dataSearch: {},
      dataList: [],
      staffForm: {
        forwardStr: undefined,
        url: undefined
      },
      editShow: false,
      disabled: false,
      id: undefined,

      formRules: {
        forwardStr: [
          { required: true, message: '请输入转发内容', trigger: 'blur' },
        ]
      },
    };
  },
  computed: {
    ...mapState('d2admin/user', ['info'])
  },
  methods: {
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    edit() {
      this.disabled = false;
    },

    formSubmit() {
      this.getStudentList()
    },

    removeSchool(index) {
      if (index != 0) {
        this.staffForm.school.splice(index, 1)
      }
      else {
        this.$message('不能删除该员工主校区')
      }
    },

    getClassList(page, pageSize, school) {
      let data = {};
      data.page = page;
      data.pageSize = pageSize;
      data.school = school;
      request({
        url: "/class/getClassList",
        method: "post",
        data
      }).then(res => {
        this.classList = []
        for (let i = 0; i < res.data.class.length; i++) {
          let key = res.data.class[i]._id;
          let label = res.data.class[i].name;
          this.classList.push({ key: key, label: label })
        }
        if (this.$route.query.id) {
          this.getStaffOne(this.$route.query.id);
        }

      });
    },
    schoolSelected() {
      this.getDepartmentList(1, 9999, this.staffForm.school);
    },
    departmentSelected() {
      // this.getClassList(1, 99999, this.staffForm.school);
      this.getReportTargetList(1, 99999);
    },
    submit() {

      let data = {};
      let url = "/message/addTargetLink";
      if (this.id != undefined) {
        data.id = this.id;
        url = "/message/updateTargetLink";
      }
      data.forwardStr = this.staffForm.forwardStr;
      data.url = this.staffForm.url;

      // data.school = this.staffForm.school
      console.log(data,'aaaaa')
      request({
        url: url,
        method: "post",
        data
      }).then(res => {
        if (res.code == 200) {
          this.refresh = true
          this.staffForm = {
            forwardStr: undefined,
            url: undefined
          }
          this.$router.push({ path: "/douyinLink/douyinLinkList" })
          this.$alert("", "提交成功", {
            confirmButtonText: "确定"
          });
        }
        // 返回数据
      });
    },
    getStaffOne(id) {
      request({
        url: "/message/getOneTargetLink",
        method: "post",
        data: { id: id }
      }).then(res => {
        if (res.code == 200) {
          this.id = res.data._id;
          this.staffForm.forwardStr = res.data.forwardStr;
          this.staffForm.url = res.data.url;
        }
      });
    },
    load() {
      this.school = this.info.school[0]._id
      this.id = this.$route.query.id
      this.staffSchool = this.school
      if ((this.info.school[0].headquarter == 1) || (this.info.school[0].headquarter == 2)) {
        this.disabled = false;
      }
      else {
        this.disabled = false
      }
      if (this.$route.query.id) {
        this.getStaffOne(this.$route.query.id);
        this.staffSchool = undefined
        // this.getSchoolList();
        this.disabled = true
        this.editShow = true;
        this.getStudentList()
      } else {
        this.getPositionList(1, 99999);
        // this.getSchoolList();
        this.getDepartmentList(1, 9999, this.info.school[0]._id);
        this.staffForm.school = [this.info.school[0]]
        this.editShow = false
        this.disabled = false
      }
    },
  },


  created() {
      if (this.$route.query.id) {
        this.getStaffOne(this.$route.query.id);
      }
  }
};
</script>
